package com.feizhi.inventory.fragment.inventory;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import com.aserbao.aserbaosandroid.functions.database.greenDao.db.DaoSession;
import com.aserbao.aserbaosandroid.functions.database.greenDao.db.TaskDetailsDao;
import com.feizhi.inventory.R;
import com.feizhi.inventory.database.Task;
import com.feizhi.inventory.database.dbApp;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.List;


public class TaskMyAdapter extends RecyclerView.Adapter <TaskMyAdapter.ViewHolder> {

    private List<Task> taskList;
    private Context context;
    Intent intent;
    private OnCliekListener onClickListener;
    private int size;
    private setCount count;
    private int positions;
    private LifecycleOwner owner;
    private boolean flag = false;
    private int count1;
    private InventoryAdapter.ItemOnLongClickListener onLongClickListener;

    public TaskMyAdapter(List<Task> taskList, Context context, LifecycleOwner owner) {
        this.taskList = taskList;
        this.context = context;
        this.owner = owner;


    }

    public TaskMyAdapter(List<Task> taskList, Context context,int size) {
        this.taskList = taskList;
        this.context = context;
        this.size = size;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_inventory, parent, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        positions = position;
//        holder.tid.setText(taskList.get(position).getT_id().toString());


        LiveEventBus.get().with("setImage", boolean.class).observe(owner, new Observer<Boolean>() {

            @Override
            public void onChanged(Boolean aBoolean) {

                flag = aBoolean;
            }
        });

        holder.create_date.setText(taskList.get(position).getCreate_time());
        holder.inventory_date.setText(taskList.get(position).getInventory_time());
        holder.count.setText("" + taskList.get(position).getCount());
        DaoSession daoSession =((dbApp) context.getApplicationContext()).getDaoSession();
        TaskDetailsDao taskDetailsDao = daoSession.getTaskDetailsDao();
        int count = (int) taskDetailsDao.queryBuilder().
                where(TaskDetailsDao.Properties.TaskID.eq(taskList.get(position).getTask_ID())).count();
//        taskList.get(position).setCount(count);
//        holder.count.setText("" + count1);
        holder.taskID.setText(taskList.get(position).getTask_ID());
//
//        LiveEventBus.get().with("setImage", int.class).observe(this, new Observer<Integer>() {
//            @Override
//            public void onChanged(Integer integer) {
//
//            }
//        });


            if (taskList.get(position).getTask_state().equals("已盘点")) {
                holder.wait.setVisibility(View.VISIBLE);
                holder.wait.setImageResource(R.mipmap.ic_completed);
//            holder.finish.setVisibility(View.VISIBLE);
            }
//            else if(taskList.get(position).getTask_state().equals("进行中")) {
//                holder.wait.setVisibility(View.VISIBLE);
//                holder.wait.setImageResource(R.mipmap.ic_inprogress);
//            }
            else {
                holder.wait.setVisibility(View.VISIBLE);
                holder.wait.setImageResource(R.mipmap.ic_incomplete);
//            holder.finish.setVisibility(View.GONE);
            }


//
        //每个对应条目的点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                intent = new Intent(context, );
//                intent.putExtra("taskID", taskList.get(position).getTask_ID());
//                holder.itemView.getContext().startActivity(intent);
                onClickListener.setItemOnClickListener(position);
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                onLongClickListener.ItemLongClickListener(position);
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {
        return taskList.size();
    }

    public void setData(List<Task> taskList) {
        this.taskList = taskList;
        notifyDataSetChanged();
    }

    public void setTime(List<Task> taskList) {
        this.taskList = taskList;
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView wait, finish;
        TextView create_date,inventory_date,count,tid, taskID;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tid = itemView.findViewById(R.id.tid);
            taskID = itemView.findViewById(R.id.taskID);
            wait = itemView.findViewById(R.id.wait);
            count = itemView.findViewById(R.id.count);
//            finish = itemView.findViewById(R.id.finish);
            create_date = itemView.findViewById(R.id.create_date);
            inventory_date = itemView.findViewById(R.id.inventory_date);
        }
    }

    /**
     * 写一个接口
     * 每个控件的点击事件
     * */
    public interface OnCliekListener {
        void setItemOnClickListener(int position);
    }

    /**
     *   创建一个公共方法
     *   暴露给外部类调用
     *
     * */
    public void setOnClickListener(OnCliekListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setCount(setCount count) {
        this.count = count;
    }

    public interface setCount {
        void setConut(int position);
    }

    public void setItemLongClickListener(InventoryAdapter.ItemOnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    //长按删除接口
    public interface ItemOnLongClickListener {

        void ItemLongClickListener(int position);

    }


}
